<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>  
<style>
    div.form-inline { margin-bottom: 5px; }
    select[name=sz] { width: 60px; }
    select[name=od] { width: 100px; }
    select[name=ss] { width: 100px; margin-left: 100px; }
    thead { background-color: #eee; }
    td:first-child, th:first-child { width: 40px; text-align: center; }
    td:nth-child(3) { width: 100px; }
    td:nth-child(4) { width: 80px; }
    td:nth-child(5) { width: 50px; text-align: center; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
    small { color: #aaa; }
</style>
<script>
$(function() {
    $("select[name=ct], select[name=sz]").change(function() {
        $("form").submit();
    })
    $("thead input[type=checkbox]").click(function() {
        $("tbody input[type=checkbox]").trigger("click");        
    })
    $("tbody tr").click(function() {
        location.href = "article.jsp" + location.search + "&aid=" + $(this).attr("data-id");
    })
    $("tbody td:nth-child(1)").click(function() {
        event.stopPropagation();
    })
    $("div.pagination a").click(function() {
        $("input[name=pg]").val($(this).attr("data-page"));
        $("form").submit();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int currentPage = param.getInt("pg", 1);
    int pageSize = param.getInt("sz", 15);
    int srchType = param.getInt("ss", 0);
    String srchText = param.getString("st", "");
    int category = param.getInt("ct", 0);
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    int userId = UserService.getCurrentUser(session).getId();
    BoardAccessRule rule = BoardAccessRuleDAO.selectByUserId(boardId, userId);
/*    if (!rule.canListArticles()) {
        response.sendRedirect("/bbs2/guest/error403.jsp");
        return;
    }*/
    Board board = BoardDAO.selectById(boardId);
    ArrayList<ArticleCategory> categories = ArticleCategoryDAO.selectAll(boardId);
    ArrayList<Article> list1 = ArticleDAO.selectNotice(boardId, userId);
    ArrayList<Article> list2 = ArticleDAO.selectPage(boardId, userId, currentPage, pageSize, srchType, srchText, category);

    int recordCount = ArticleDAO.getRecordCount(boardId, srchType, srchText, category);
    int pageCount = (recordCount + pageSize - 1 ) / pageSize;
    int basePage = ((currentPage - 1) / 10) * 10;
    
    String urlCreate = "create.jsp?" + request.getQueryString();
%>
<body>
<%@ include file="../_menu.jsp" %>  

<div class="container">

    <h1><%= board.getTitle() %></h1>
    <hr />
    
    <form>
        <input type="hidden" name="bid" value="<%= boardId %>" />
        <input type="hidden" name="pg" value="1" />

        <div class="pull-right">
            <%
                if (rule.canCreateArticle()) {
            %>
                <a href="<%= urlCreate %>" class="btn btn-small btn-primary">글쓰기</a>
            <% } %>
        </div>
        
        <div class="form-inline">
            분류:
            <select name="ct">
                <option value="0">전체</option>
                <% for (ArticleCategory ac : categories) { %>
                    <option value="<%= ac.getId() %>" <%= category==ac.getId() ? "selected" : "" %>><%= ac.getName() %></option>
                <% } %>                    
            </select>

            <select name="ss">
                <option value="0" <%= srchType==0 ? "selected" : "" %>>검색없음</option>
                <option value="1" <%= srchType==1 ? "selected" : "" %>>제목</option>
                <option value="2" <%= srchType==2 ? "selected" : "" %>>사용자</option>
                <option value="3" <%= srchType==3 ? "selected" : "" %>>본문</option>
            </select>

            <input type="text" name="st" value="<%= srchText %>" />
            <button type="submit" class="btn btn-small search">검색</button>
        </div>
        
        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th>no</th>
                    <th>제목</th>
                    <th>작성자</th>
                    <th>작성일</th>
                    <th>조회수</th>
                </tr>
            </thead>
            <tbody>
                <% for (Article article : list1) { %>
                    <tr data-id="<%=article.getId()%>">
                        <td><span class="label label-important">공지</span></td>
                        <td><small><%= article.getCategory() %>:</small> <%= article.getTitle() %></td>
                        <td><%= article.getName() %></td>
                        <td><%= DateUtil.toYMD(article.getWriteTime()) %></td>
                        <td><%= article.getTotalReadCount() %></td>
                    </tr>
                <% } %>
                <% for (Article article : list2) { %>
                    <tr data-id="<%=article.getId()%>">
                        <td><%= article.getNo() %></td>
                        <td><% if (article.getIndentation() > 0) { %>
                                <img style="display:inline-block; margin-left: <%= 10 * article.getIndentation() %>px" src="/bbs2/res/images/reply.png" />
                            <% } %>
                            <small><%= article.getCategory() %>:</small>
                            <%= article.getTitle() %>
                            <% if (article.getCommentCount() > 0) { %>
                                <small>[<%= article.getCommentCount() %>]</small>
                            <% } %>
                            <% if (article.getMyReadCount() == 0) { %> 
                                <img src="/bbs2/res/images/new.png" />
                            <% } %>
                        </td>
                        <td><%= article.getName() %></td>
                        <td><%= DateUtil.toYMD(article.getWriteTime()) %></td>
                        <td><%= article.getTotalReadCount() %></td>
                    </tr>
                <% } %>
            </tbody>
        </table>

        <div class="controls pull-right form-inline">
            화면크기:             
            <select name="sz">
                <option <%= pageSize==10 ? "selected" : "" %>>10</option>
                <option <%= pageSize==15 ? "selected" : "" %>>15</option>
                <option <%= pageSize==30 ? "selected" : "" %>>30</option>
                <option <%= pageSize==60 ? "selected" : "" %>>60</option>
                <option <%= pageSize==100 ? "selected" : "" %>>100</option>
            </select>
        </div>
        
        <div class="pagination pagination-small pagination-centered">
            <ul>
                <% if (basePage > 0) { %>
                    <li><a data-page="<%= basePage %>">Prev</a></li>                     
                <% } %>
                <% for (int i=1; i <= 10 && basePage + i <= pageCount; ++i) { %>
                    <li class='<%= basePage+i==currentPage ? "active" : "" %>'><a data-page="<%= basePage+i %>"><%= basePage + i %></a></li>
                <% } %>
                <% if (basePage + 11 <= pageCount) { %>
                    <li><a data-page="<%= basePage+11 %>">Next</a></li>                     
                <% } %>
            </ul>
        </div>
                
    </form>

</div>

<%@ include file="../_footer.jsp" %>  
</body>
</html>
